var request = null;

// The prototype.php starts at 1
var pages = 2;


function createRequest() {
    try {
	request = new XMLHttpRequest();
    } catch (trymicrosoft) {
	try {
	    request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
	    try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
		request = null;
	    }
	}
    }

    if (request == null)
	alert("Error creating request object!");
}




	

/* Changes a specific image in the document */
function changeImage() {

    if(request.readyState == 4){
	if (request.status == 200) {
	    /* Gets the next image */
	    var xmlDoc = request.responseXML;

	    var xmlImg = xmlDoc.getElementsByTagName("image")[0];

	    var img = xmlImg.firstChild.nodeValue;

	    document.getElementById('catimg').src = img;
	    document.getElementById('name').value = "";

	    var tabooword;
	    var xmlTaboo;
	    var len = xmlDoc.getElementsByTagName("tabooword").length;
	    for(var counter =0;counter< len;counter++){
		xmlTaboo = xmlDoc.getElementsByTagName("tabooword")[counter];
		tabooword = xmlTaboo.firstChild.nodeValue;

		document.getElementById("taboo"+counter).innerHTML = "<h4>"+tabooword+"</h4>";
	   }
	   

	   for (var index = len; index < 6; index++) {
	   	document.getElementById("taboo"+index).innerHTML = "<h4>&nbsp;</h4>";
	   }
		
	}
    }
}






function passImage(){
    var word =  document.getElementById('catimg').src;
    var content ="application/x-www-form-urlencoded";
    request.setRequestHeader("Content-Type",content);		     
    /* For some reason anymore vars start with & */
    request.send("name="+word);

}



function sendRequest(){

    //request = null;
    createRequest();
    request.onreadystatechange =changeImage;
    var url = "getimages.php";
    request.open("POST", url, true);
    //request.send(null);
    var page=document.getElementById("currentattempt").value;

    var content ="application/x-www-form-urlencoded";
    request.setRequestHeader("Content-Type",content);

    request.send("current="+page);

}

function getWord(){
    return document.getElementById('name').value;
}


function check(e){
var key;
var string;

/**
 * Removal of last character
 * var myStr = "One, Two, Three, Four,"
 * var strLen = myStr.length;
 * myStr = myStr.slice(0,strLen-1); 
 */


    	if(window.event){ // IE 
  		key = e.keyCode;
	}else if(e.which){ // Netscape/Firefox/Opera
  		key = e.which;
	}
	
	
	if(key == 13){
		labelImage();
		return false;
	}
	
		
}

/* Coding is awful*/
function labelImage(){
   var string = document.getElementById("name").value;
 

    var the_char=string.charAt(string.length-1);


    check(the_char);


    if(sendWord() == 0){

	if(pages < 6){
	    sendRequest();
	}else{
	    //alert ("finished");
	    window.location = "endofprocess.php"

	}
	
    }
    
       /* Keeps track of the current guess */
       pages++;
       document.getElementById("currentattempt").value = pages;
       //alert(document.getElementById("currentattempt").value);
}

function isTabooWord(word){
    
    var counter = 0;
    var curr = document.getElementById("taboo"+counter).innerHTML;
 
    while ((curr !="<h4>&nbsp;</h4>")&&(counter < 6)){
	// Have to add the internal tags to get match

	if(curr == "<h4>"+word+"</h4>"){
	    
	    return 1;
	}
	curr = document.getElementById("taboo"+counter).innerHTML;
	counter++;
    }

    return 0;
}

function passImage(){
    createRequest();
    //request.onreadystatechange = getWord;
    var url = "sendpasses.php";
    request.open("POST", url, true);
    
    var pic = document.getElementById("catimg").src;
    var content ="application/x-www-form-urlencoded";
    request.setRequestHeader("Content-Type",content);
    request.send("picture="+pic);
    sendRequest();

}



function sendWord(){
    createRequest();
    request.onreadystatechange = getWord;
    var url = "sendWords.php";
    request.open("POST", url, true);
    var word = getWord();
    word = word.toLowerCase();

    if(isTabooWord(word) == 1){
	alert("You can not use a taboo word");
	return 1;
    } 


    // String is not empty
    if(word.length > 0){
	
	var content ="application/x-www-form-urlencoded";
	var current =document.getElementById("currentattempt").value;
	var image = document.getElementById('catimg').src;
	request.setRequestHeader("Content-Type",content);
	

	
	var counter = 0;
	var checkWord = document.getElementById("taboo"+counter).innerHTML;
	while((checkWord !="<h4>&nbsp;</h4>") && (counter < 6)){
		checkWord = document.getElementById("taboo"+counter).innerHTML;
		counter++;
	}
	      
	/* For some reason anymore vars start with & */
	request.send("name="+word+"&current="+counter+"&picture="+image);
    }else{
	alert("Sorry the string you entered  was empty");
	return 1;
    }

    //alert("Returns Correctly");
    return 0;
}
